package DAO;


import Logica.Pet;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AppoinmentDao {
  public AppoinmentDao() {
  }

  public boolean creaApp(Date fecha, String tipoCita, int clinicNumber,
                         int ownerNumber, int hs, int mins,
                         int dura, int pet) {

    Conexion conexion = new Conexion();
    boolean creado = false;
    Connection conex;

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;
      int numberApp=-1;
      ResultSet rsa = null;
      int mayor = -1;
      ps =
          conex.prepareStatement("select max (APPOINTNUMBER)  from APPOINTMENT");
      rsa = ps.executeQuery();
      if (rsa != null) {
        while (rsa.next()) {
          mayor = rsa.getInt(1);
        }
      } else
        numberApp = 1;

      ps =
          conex.prepareStatement("INSERT INTO APPOINTMENT (APPOINTNUMBER, DATEA, TYPEA, CLINICNUMBER, OWNERNUMBER, HOURSTART, MINSTART, DURACION, PETID) VALUES (?, ?, ?,?,?, ?, ?, ?, ?)");
      numberApp = mayor+1;
      ps.setInt(1, numberApp);
      ps.setDate(2, fecha);
      ps.setString(3, tipoCita);
      ps.setInt(4, clinicNumber);
      ps.setInt(5, ownerNumber);
      ps.setInt(6, hs);
      ps.setInt(7, mins);
      ps.setInt(8, dura);
      ps.setInt(9, pet);

      int numeroFilas = ps.executeUpdate();
      System.out.println("El numero de filas que inserto fueron" +
                         numeroFilas);
      creado = true;
      conex.close();

    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());

    }
    return creado;
  }
  
    public boolean eliminarApp(int numberApp) {

      Conexion conexion = new Conexion();
      boolean eliminado = false;
      Connection conex;

      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;

        ps = conex.prepareStatement("DELETE FROM APPOINTMENT where AppointNumber = ?");

        ps.setInt(1, numberApp);


        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que borro fueron" +
                           numeroFilas);
        eliminado = true;
        conex.close();

      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());

      }
      return eliminado;
    }
    
    public boolean modificaAppFecha(int numberA,  Date fecha)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Appointment set DATEA = ?  where AppointNumber = ?"); 
        
        ps.setDate(1, fecha);
        ps.setInt(2, numberA);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaAppTipo(int numberA,String tipo)
    {
      String cambio=" ";
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        ResultSet rs=null;   
        
           ps =
            conex.prepareStatement("UPDATE Appointment set typeA = ?  where AppointNumber = ?");  
        ps.setString(1, tipo);
          
        ps.setInt(2, numberA);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaAppClinicNumber(int numberA,  int number)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Appointment set CLINICNUMBER = ?  where AppointNumber = ?"); 
        
        ps.setInt(1, number);
        ps.setInt(2, numberA);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaAppOwnerNumber(int numberA,  int number)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Appointment set OwnerNumber = ?  where AppointNumber = ?"); 
        
        ps.setInt(1, number);
        ps.setInt(2, numberA);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaAppHoracomienzo(int numberA,  int hour)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Appointment set HOURSTART = ?  where AppointNumber = ?"); 
        
        ps.setInt(1, hour);
        ps.setInt(2, numberA);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaAppMinStart(int numberA,  int min)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Appointment set MINStart = ?  where AppointNumber = ?"); 
        
        ps.setInt(1, min);
        ps.setInt(2, numberA);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaAppDuracion(int numberA,  int dur)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Appointment set Duracion = ?  where AppointNumber = ?"); 
        
        ps.setInt(1, dur);
        ps.setInt(2, numberA);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
    public boolean modificaAppPetId(int numberA,  int pet)
    {
      Conexion conexion = new Conexion();
      Connection conex;
      try {
        conex = conexion.getConnection();
        PreparedStatement ps = null;
        
        ps =
            conex.prepareStatement("UPDATE Appointment set PetId = ?  where AppointNumber = ?"); 
        
        ps.setInt(1, pet);
        ps.setInt(2, numberA);
        
        int numeroFilas = ps.executeUpdate();
        System.out.println("El numero de filas que actualizo fueron" +
                           numeroFilas);

        conex.close();
        
      } catch (SQLException ex) {
        System.err.println("SQLException: " + ex.getMessage());
        return false;
      }
      return true;
    }
    
  public Object[][] DatosCitaTabla(int clinincnumber) {
    Conexion conexion = new Conexion();
    Connection conex;
    Object[][] datos = new Object[100][100];

    try {
      conex = conexion.getConnection();
      PreparedStatement ps = null;
      ResultSet rs = null;

      ps = conex.prepareStatement("select * FROM Appointment where clinicnumber = ?");     
      int i = 1;
      int j = 0;
      ps.setInt(1,clinincnumber);
      rs = ps.executeQuery();
      System.out.println(rs + " es el resultado de la consulta !");

      while (rs.next()) {
      
        datos[0][0]= " APPOINMENT NUMBER";
        datos[0][1]="DATE";
        datos[0][2]="TYPEA";
        datos[0][3]= "CLINIC NUMBER";
        datos[0][4]="OWNER NAME";
        datos[0][5]= "HOUR START";
        datos[0][6]="MIN START";
        datos[0][7]="DURACION";
        datos[0][8]="PET ID";
        
        datos[i][j] = rs.getInt(1);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getDate(2);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getString(3);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getInt(4);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getInt(5);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getInt(6);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getInt(7);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getInt(8);
        System.out.print(datos[i][j] + " ");
        j++;
        datos[i][j] = rs.getInt(9);
        System.out.print(datos[i][j] + " ");
        System.out.println("FIN!! ");
        i++;
        j=0;
      }
      
      rs.close();
      ps.close();
      conex.close();
      
    } catch (SQLException ex) {
      System.err.println("SQLException: " + ex.getMessage());
    }
    return datos;
  }
}
